package com.bank.user.service.impl;

import com.bank.common.commonUtil.AuthContextHolderUtil;
import com.bank.common.exceptionUtil.CustomedException;
import com.bank.common.resultUtil.ResultCodeEnum;
import com.bank.model.entity.Account;
import com.bank.model.entity.UserAccount;
import com.bank.model.vo.account.AccountUpdatePasswordVo;
import com.bank.model.vo.account.AccountVo;
import com.bank.user.service.AccountService;
import com.bank.user.util.EntityFactory;
import com.bank.user.util.MapperFactory;
import com.bank.user.util.VoFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Cejay1314
 * @version 1.0
 * @create 2023/3/27 16:41
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    MapperFactory mapperFactory;
    @Autowired
    EntityFactory entityFactory;
    @Autowired
    VoFactory voFactory;

    public void createAccount(AccountUpdatePasswordVo accountUpdatePasswordVo, Long userId){
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserAccount> userAccounts = mapperFactory.getUserAccountMapper().selectList(queryWrapper);
        List<String> accountIds = new ArrayList<>();
        for(UserAccount userAccount:userAccounts){
            accountIds.add(userAccount.getAccountId());
        }
        if(accountIds.size()>3){
            throw new CustomedException(ResultCodeEnum.FAIL);
        }
        else{
            Date date = new Date();
            StringBuilder cardNumber = new StringBuilder();
            cardNumber.append(date.getYear());
            cardNumber.append(date.getMonth());
            cardNumber.append(date.getDay());
            cardNumber.append(date.getHours());
            cardNumber.append(date.getMinutes());
            cardNumber.append(date.getSeconds());
            Account account = entityFactory.getAccount(cardNumber.toString(),accountUpdatePasswordVo.getPassword());
            if(mapperFactory.getAccountMapper().insert(account)<1){
                throw new CustomedException(ResultCodeEnum.FAIL);
            }
            else{
                UserAccount userAccount = entityFactory.getUserAccount(userId,account.getAccountId());
                mapperFactory.getUserAccountMapper().insert(userAccount);
            }
        }
    }

    @Override
    public List<AccountVo> getAccountsVo(Long userId) {
        List<Account> accounts = getAccounts(userId);
        List<AccountVo> accountVos = new ArrayList<>();
        for(Account account:accounts){
            accountVos.add(voFactory.getAccountVo(account));
        }
        return accountVos;
    }

    public List<Account> getAccounts(Long userId){
        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<UserAccount> userAccounts = mapperFactory.getUserAccountMapper().selectList(queryWrapper);
        List<String> accountIds = new ArrayList<>();
        for(UserAccount userAccount:userAccounts){
            accountIds.add(userAccount.getAccountId());
        }
        List<Account> accounts = getAccounts(accountIds);
        return accounts;
    }
    public Account getAccount(String accountId){
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountId);
        Account account = mapperFactory.getAccountMapper().selectOne(queryWrapper);
        return account;
    }
    @Override
    public List<Account> getAccounts(List<String> accountIds) {
        if(accountIds ==null || accountIds.size()==0){
            return new ArrayList<Account>();
        }
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("account_id",accountIds);
        List<Account> accounts = mapperFactory.getAccountMapper().selectList(queryWrapper);
        return accounts;
    }

    @Override
    public void updataAccount(Account account) {
        if(mapperFactory.getAccountMapper().updateById(account)<1){
            throw new CustomedException(ResultCodeEnum.FAIL);
        }
    }

    public void delAccount(AccountUpdatePasswordVo accountUpdatePasswordVo,Long userId){
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account_id",accountUpdatePasswordVo.getAccountId());
        if(mapperFactory.getAccountMapper().delete(queryWrapper)<1){
            throw new CustomedException(ResultCodeEnum.FAIL);
        }
        QueryWrapper<UserAccount> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("user_id",userId);
        mapperFactory.getUserAccountMapper().delete(queryWrapper1);
    }


}
